<?php

class ReportController extends Controller
{
	public function actionAdd()
	{
		$this->render('add');
	}
	
	public function actionDelete($id)
	{
		$doc=Document::model()->findByPk($id);		
		$path = Yii::app()->basePath.'/../document/'.$doc->fileName;
		unlink($path);
		$doc->docName='';
		$doc->fileName='';
		if($doc->save()){
			$this->redirect(array('list','id'=>$doc->projectID));
		}else{
			throw new CHttpException(404,'The requested page does not exist.');
		}		
	}

	public function actionList($id)
	{
		$this->layout='//layouts/blank';		
		$project = Project::model()->findByPk($id);		
		$noEnableAnyReport="";		
		$enReport1="";
		$enReport2="";
		$enReport3="";		
		$enDelUploadReport1="";		
		$enDelUploadReport2="";		
		$enDelUploadReport3="";
		
		if($project->statusCode == "step1"){
			$enReport1="YES";
			$enReport2="NO";
			$enReport3="NO";
			$enAnyReport="YES";
		}
		elseif($project->statusCode== "step2"){
			$enReport1="YES";
			$enReport2="YES";
			$enReport3="NO";
			$enAnyReport="YES";
		}
		elseif($project->statusCode=="step3"){
			$enReport1="YES";
			$enReport2="YES";
			$enReport3="YES";
			$enAnyReport="YES";
		}
		elseif($project->statusCode=="step4"){
			$enReport1="YES";
			$enReport2="YES";
			$enReport3="NO";
			$enAnyReport="YES";
		}
		elseif($project->statusCode=="step5"){
			$enReport1="YES";
			$enReport2="YES";
			$enReport3="YES";
			$enAnyReport="YES";
		}
		elseif($project->statusCode=="step6"){
			$enReport1="YES";
			$enReport2="YES";
			$enReport3="NO";
			$enAnyReport="YES";
		}
		elseif($project->statusCode=="step7"){
			$enReport1="YES";
			$enReport2="YES";
			$enReport3="NO";
			$enAnyReport="YES";
		}
		elseif($project->statusCode=="step8"){
			$enReport1="YES";
			$enReport2="YES";
			$enReport3="YES";
			$enAnyReport="YES";
		}
		elseif($project->statusCode=="step9"){
			$enReport1="YES";
			$enReport2="YES";
			$enReport3="NO";
			$enAnyReport="YES";
		}
		elseif($project->statusCode=="step10"){
			$enReport1="YES";
			$enReport2="YES";
			$enReport3="YES";
			$enAnyReport="YES";
		}
		elseif($project->statusCode=="step11"){
			$enReport1="YES";
			$enReport2="YES";
			$enReport3="NO";
			$enAnyReport="YES";
		}
		else{
			$enReport1="NO";
			$enReport2="NO";
			$enReport3="NO";
			$enAnyReport="NO";
		}
		//$enAnyReport="NO";
		if($project->documents == NULL){
			$enDelUploadReport1="UPLOAD";
			$enDelUploadReport2="UPLOAD";
			$enDelUploadReport3="UPLOAD";
		}
		else{
			foreach($project->documents as $doc){
				if($doc->typeID == 1 or $doc->typeID == 2 ){
					if($doc->fileName != null){
						$enDelUploadReport1="DEL";
					}
					else{
						$enDelUploadReport1="UPLOAD";
					}
				}
				elseif($doc->typeID == 3 or $doc->typeID == 4 ){
					if($doc->fileName != null){
						$enDelUploadReport2="DEL";
					}
					else{
						$enDelUploadReport2="UPLOAD";
					}
				}
				elseif($doc->typeID == 5 or $doc->typeID == 6 ){
					if($doc->fileName != null){
						$enDelUploadReport3="DEL";
					}
					else{
						$enDelUploadReport3="UPLOAD";
					}
				}
			}
		}		
		
		$role=Yii::app()->session->get('USERTYPE');
		
		$this->render('list',array(
				'usertype'=>$role,
				'project'=>$project,
				'noEnableAnyReport'=>$noEnableAnyReport,
				'enReport1'=>$enReport1,
				'enReport2'=>$enReport2,
				'enReport3'=>$enReport3,
				'enDelUploadReport1'=>$enDelUploadReport1,
				'enDelUploadReport2'=>$enDelUploadReport2,
				'enDelUploadReport3'=>$enDelUploadReport3,
				'enAnyReport'=>$enAnyReport,
			));
	}
	/*
	public function actionPrint()
	{
		$doc= new Document();
		$doc->unsetAttributes();
		$doc->typeID=$_POST['reportTypeId'];
		$doc->projectID=$_POST['reportOfProjectId'];
		$doc->documentDate=$_POST['txtInputDateReport'];		
		$doc->save();
		//print_r($doc);
		//exit;
		$this->redirect(array('list','id'=>$doc->projectID));	
	}
	*/
	public function actionPrint($projectID,$reportTypeID,$reportDate)
	{
		$project = Project::model()->findByPk($projectID);
		$doc= new Document();
		$doc->unsetAttributes();
		$doc->typeID=$reportTypeID;
		$doc->projectID=$projectID;
		$doc->documentDate=$reportDate;
		$doc->save();
		//$this->render('print');
		//$this->redirect(array('/jasperReport/report1','id'=>$doc->documentID));
		if($doc->typeID==1){
			if(count($project->teachers) > 1 ){
				$this->redirect(array('/jasperReport/report1/'.$doc->documentID.'?type=2'));
			}
			else {
				$this->redirect(array('/jasperReport/report1/'.$doc->documentID.'?type=1'));
			}				
		}
		else if($doc->typeID==3){
			$this->redirect(array('/jasperReport/report1/'.$doc->documentID.'?type=3'));
		}
		if($doc->typeID==5){
			$this->redirect(array('/jasperReport/report1/'.$doc->documentID.'?type=4'));
		}		
	}
	
	public function actionView($id)
	{
		$model = Document::model()->findByPk($id);
		$this->render('view',array('filename'=>$model->fileName,));
	}
	
	public function actionUpload()
	{
		$model = Document::model()->findByPk($_POST['fileDocumentID']);
		//print_r($model);
		//exit;
		if($model === null){			
			$model = Document::model()->findAll('projectID=:projectID AND typeID=:typeID',array(':projectID'=>$_POST['fileProjectID'],':typeID'=>$_POST['fileReportTypeID']));
			
			if(sizeof($model)==0){
				/*
				print_r("Error on if ---->");
				print_r($model);
				print_r($_POST['fileReportUpload']);
				exit;
				*/
				$model = new Document();
				$model->unsetAttributes();
				$model->typeID=$_POST['fileReportTypeID'];
				$model->projectID=$_POST['fileProjectID'];
				$model->docName=$_POST['fileDocumentName'];
				$model->save();
				$tmp_file_suffix=explode("/",$_FILES['fileReportUpload']['type']);
				//$tmp_file_name=$model->documentID."-".date("YYmmddHHiiss").'.'.$tmp_file_suffix[1];
				//$tmp_file_name=$model->documentID."-".date("YYmmddHHiiss").'.'.$tmp_file_suffix[1];
				$tmp_file_name=$model->documentID."-".date("YYmmddHHiiss").'.pdf';
				$model->fileName=$tmp_file_name;
				$path = Yii::app()->basePath.'/../document/'.$tmp_file_name;
				//print_r($model);
				//exit;
				if (copy($_FILES['fileReportUpload']['tmp_name'], $path)){
				//$this->redirect(array('list','id'=>$model->projectID));
				$model->save();
				echo "<script>parent.showReport(".$_POST['fileProjectID'].");</script>";
				}
			}
			else{
				/*
				print_r("Error on if -> else---->");
				print_r($model);
				print_r($_POST['fileReportUpload']);
				exit;
				*/
				foreach($model as $modelItem){
					$modelItem->docName=$_POST['fileDocumentName'];
					$tmp_file_suffix=explode("/",$_FILES['fileReportUpload']['type']);
					//$tmp_file_name=$modelItem->documentID."-".date("YYmmddHHiiss").'.'.$tmp_file_suffix[1];
					//$tmp_file_name=$model->documentID."-".date("YYmmddHHiiss").'.'.$tmp_file_suffix[1];
					$tmp_file_name=$model->documentID."-".date("YYmmddHHiiss").'.pdf';
					$modelItem->fileName=$tmp_file_name;
					$path = Yii::app()->basePath.'/../document/'.$tmp_file_name;
					//print_r($model);
					//exit;
					copy($_FILES['fileReportUpload']['tmp_name'], $path);
					if($modelItem->save()){
						echo "<script>parent.showReport(".$_POST['fileProjectID'].");</script>";
					}
				}
			}
		}else{
			/*
			print_r("Error on else---->");
			print_r($_POST['fileReportUpload']);
			*/
			$model->docName=$_POST['fileDocumentName'];			
			$tmp_file_suffix=explode("/",$_FILES['fileReportUpload']['type']);
			//$tmp_file_name=$model->documentID."-".date("YYmmddHHiiss").'.'.$tmp_file_suffix[1];
			$tmp_file_name=$model->documentID."-".date("YYmmddHHiiss").'.pdf';
			$model->fileName=$tmp_file_name;
			$path = Yii::app()->basePath.'/../document/'.$tmp_file_name;
			//print_r($model);
			//exit;			
			//print_r($_FILES['fileReportUpload']);
			//echo $path;
			//exit;
			copy($_FILES['fileReportUpload']['tmp_name'], $path);
			if($model->save()){
				echo "<script>parent.showReport(".$_POST['fileProjectID'].");</script>";
			}
		}
	}
}